package com.xhh.nphm.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhh.nphm.common.core.domain.entity.Dept;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.system.domain.IsGoBackConfig;
import com.xhh.nphm.system.mapper.IsGoBackConfigDao;
import com.xhh.nphm.system.service.IsGoBackConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class IsGoBackConfServiceImpl extends ServiceImpl<IsGoBackConfigDao,IsGoBackConfig> implements IsGoBackConfService {
    @Autowired
    private IsGoBackConfigDao isGoBackConfigDao;

    @Override
    public boolean deleteDeptConfByDeptId(Long DeptId) {
        return isGoBackConfigDao.deleteDeptConfByDeptId(DeptId) >= 0;
    }

    @Override
    @Transactional
    public boolean insertDeptConf(Long deptId, List<String> noGoBackDeptIds) {
        int row = 0;
        for (String noGoBackDeptId : noGoBackDeptIds) {
            isGoBackConfigDao.insertDeptConf(deptId,noGoBackDeptId);
            ++ row;
        }
        return row >= 0;
    }

    @Override
    public IsGoBackConfig selectConfByDept(Long deptId) {
        return isGoBackConfigDao.selectConfByDept(deptId);
    }

    @Override
    public boolean saveOrUpdateConf(IsGoBackConfig isGoBackConfig, SysUser user) {
        int row = 0;
        IsGoBackConfig oldConfig = isGoBackConfigDao.selectConfByDept(user.getDeptId());
        if (oldConfig != null){
            //允许默认全部部门
            oldConfig.setIsAll(isGoBackConfig.getIsAllow() == 0 ? 100 : isGoBackConfig.getIsAll());
            oldConfig.setIsAllow(isGoBackConfig.getIsAllow());
            oldConfig.setOptTime(new Date());
            row = isGoBackConfigDao.updateById(oldConfig);
        }else{
            row = isGoBackConfigDao.insert(isGoBackConfig);
        }
        return row > 0;
    }

    @Override
    public String selectNoGoBackDeptNames(Long deptId) {
        return isGoBackConfigDao.selectNoGoBackDeptNames(deptId);
    }

    @Override
    public int checkPeerDeptIsGoBack(String sdeptId, String sbdeptId) {
        IsGoBackConfig isGoBackConfig = isGoBackConfigDao.selectConfByDept(Long.valueOf(sdeptId));
        if (isGoBackConfig != null){
            if (isGoBackConfig.getIsAllow() == 0){
                return 0;//允许退回
            }else if (isGoBackConfig.getIsAllow() == 1){
                if (isGoBackConfig.getIsAll() == 100){
                    return 1;//不允许退回
                }else if(isGoBackConfig.getIsAll() == 101){
                    if (isGoBackConfigDao.checkPeerDeptIsGoBack(sdeptId, sbdeptId) >= 0) {
                        return 1;
                    }else{
                        return 0;
                    }
                }
            }
        }
        return 0;
    }

    @Override
    public int checkPeerDeptIsGoBack(Long deptId) {
        return isGoBackConfigDao.checkIsGoBackByDeptId(deptId) > 0 ? 1 : 0;
    }
}
